package com.gherasoft.view;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.gherasoft.adapter.OpenFileAdapter;
import com.gherasoft.model.CheckListModel;
import com.gherasoft.model.FolderItem;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;

/**
 * User: sckomoroh
 * Date: 3/22/13
 * Time: 9:42 AM
 */
public class OpenFileActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.open_file);

        final OpenFileAdapter adapter = new OpenFileAdapter(this);

        ListView fileListView = (ListView)findViewById(R.id.openFileListView);
        final TextView currentDirTextView = (TextView)findViewById(R.id.currentDirTextView);

        fileListView.setAdapter(adapter);
        currentDirTextView.setText(adapter.getCurrentFolder());

        fileListView.setOnItemClickListener(new OnItemClickListener(adapter, currentDirTextView));
    }

    private void tryParseFile(String fileName)
    {
        try
        {
            CheckListModel model = new CheckListModel(null);

            FileInputStream fileInputStream = new FileInputStream(fileName);
            BufferedReader reader = new BufferedReader(new InputStreamReader(fileInputStream));

            String line;
            while ((line = reader.readLine()) != null)
            {
                if (model.getListName() == null)
                {
                    int start = line.indexOf("[");
                    int end = line.indexOf("]");

                    if (start != -1 && end != -1 && start < end)
                    {
                        model.setListName(line.substring(start + 1, end));
                    }
                }
                else
                {
                    model.addItem(line);
                }
            }

            reader.close();

            MainActivity.addModel(model);
            model.setSaved();
            model.saveList(this.getApplicationInfo().dataDir);

            onBackPressed();

            int index = MainActivity.findModelIndex(model);

            Intent intent = new Intent(OpenFileActivity.this, RunListActivity.class);
            intent.putExtra("model_index", index);
            startActivity(intent);

        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            Toast toast = Toast.makeText(this, "Could not import list", Toast.LENGTH_SHORT);
            toast.show();
        }
    }

    private class OnItemClickListener implements AdapterView.OnItemClickListener
    {
        private OpenFileAdapter adapter;
        private TextView currentFolderTextView;

        public OnItemClickListener(OpenFileAdapter adapter, TextView currentFolderTextView)
        {
            this.adapter = adapter;
            this.currentFolderTextView = currentFolderTextView;
        }

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
        {
            FolderItem folderItem = (FolderItem)this.adapter.getItem(i);
            if (folderItem.isFolder())
            {
                this.adapter.tryRebuild(i);
                this.currentFolderTextView.setText(adapter.getCurrentFolder());
            }
            else
            {
                String fileName = this.adapter.getFullName(i);
                OpenFileActivity.this.tryParseFile(fileName);
            }
        }
    }
}
